package com.xiaoxiong.gobang.ui;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import com.xiaoxiong.gobang.R;
import com.xiaoxiong.gobang.bean.Player;

/**
 * @author xiaoxiong
 * @Package com.xiaoxiong.gobang.ui
 * @Description:
 * @email 249900679@qq.com
 * @date 2016/4/3 10:26
 */
public class GameView2 extends SurfaceView implements SurfaceHolder.Callback {

    private Context mContext;
    private SurfaceHolder mSurfaceHolder;
    private Canvas mCanvas;
    private int ViewWidth;

    private Bitmap CheckerBoardBitmap;
    private Bitmap BlackChessBitmap;
    private Bitmap WhiteChesBitmap;
    private Bitmap SelectedBitmap;

    private Bitmap background;
    private Bitmap blackchess;
    private Bitmap whitechess;
    private Bitmap Selectedchess;

    private int[] checkerBoardWH;
    private int[] chessWH;
    private int[] SelectedWH;

    private float scale;
    private Matrix matrix;

    private int[] m_board;

    public GameView2(Context context) {
        super(context);
        this.mContext = context;
        getHolder().addCallback(this);
    }

    public GameView2(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        getHolder().addCallback(this);
    }

    public GameView2(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        getHolder().addCallback(this);
    }

    private void Init() {
        CheckerBoardBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.board);
        BlackChessBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.black);
        WhiteChesBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.white);
        SelectedBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.selected);

        checkerBoardWH = new int[2];
        checkerBoardWH[0] = CheckerBoardBitmap.getWidth();
        checkerBoardWH[1] = CheckerBoardBitmap.getHeight();
        chessWH = new int[2];
        chessWH[0] = BlackChessBitmap.getWidth();
        chessWH[1] = BlackChessBitmap.getHeight();
        SelectedWH = new int[2];
        SelectedWH[0] = SelectedBitmap.getWidth();
        SelectedWH[1] = SelectedBitmap.getHeight();

        matrix = new Matrix();
        m_board = new int[15 * 15 + 34 + 15];
        for (int i = 0; i < 15; ++i) {
            for (int j = 0; j < 15; ++j) {
                m_board[17 + i + j * 16] = -1;
            }
        }
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        Init();
        Log.d("view", "-------------");
        mSurfaceHolder = holder;
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        mSurfaceHolder = holder;
        this.ViewWidth = width;
        Log.d("view", ViewWidth + "");
        this.scale = ((float) ViewWidth) / checkerBoardWH[0];
        matrix.postScale(scale, scale);
        background = Bitmap.createBitmap(CheckerBoardBitmap, 0, 0, checkerBoardWH[0], checkerBoardWH[1], matrix, true);
        blackchess = Bitmap.createBitmap(BlackChessBitmap, 0, 0, chessWH[0], chessWH[1], matrix, true);
        whitechess = Bitmap.createBitmap(WhiteChesBitmap, 0, 0, chessWH[0], chessWH[1], matrix, true);
        Selectedchess = Bitmap.createBitmap(SelectedBitmap, 0, 0, chessWH[0], chessWH[1], matrix, true);
        Draw();
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {

    }

    private void Draw() {
        mSurfaceHolder = getHolder();
        try {
            mCanvas = null;
            synchronized (mSurfaceHolder) {
                mCanvas = mSurfaceHolder.lockCanvas(null);
                mCanvas.drawColor(Color.WHITE);
                mCanvas.drawBitmap(background, 0, 0, null);

                for (int i = 0; i < m_board.length; ++i) {
                    int pos = i - 17;
                    if(m_board[i] > 5){
                        int cx = pos % 16;
                        int cy = (pos - cx) / 16;
                        mCanvas.drawBitmap(Selectedchess, cx * ViewWidth / 15 + 5 * scale, cy * ViewWidth / 15 + 6 * scale, null);
                    }
                    if (m_board[i] == Player.FIRSTPLAYER || m_board[i] - 5 == Player.FIRSTPLAYER) {
                        int cx = pos % 16;
                        int cy = (pos - cx) / 16;
                        mCanvas.drawBitmap(blackchess, cx * ViewWidth / 15 + 5 * scale, cy * ViewWidth / 15 + 6 * scale, null);
                    } else if (m_board[i] == Player.SECONDPLAYER || m_board[i] - 5 == Player.SECONDPLAYER) {
                        int cx = pos % 16;
                        int cy = (pos - cx) / 16;
                        mCanvas.drawBitmap(whitechess, cx * ViewWidth / 15 + 5 * scale, cy * ViewWidth / 15 + 6 * scale, null);
                    }
                }
            }
        } finally {
            if (mCanvas != null) {
                mSurfaceHolder.unlockCanvasAndPost(mCanvas);
            }
        }
    }

    public void setBoard(int[] m_board) {
        this.m_board = m_board;

        this.Draw();
    }

    public int getPos(float x, float y) {
        int ix = (int) ((x - 5 * scale) * 1.0 / ViewWidth * 15);
        int iy = (int) ((y - 6 * scale) * 1.0 / ViewWidth * 15);
        Log.d("pos", ix + " " + iy + " " + ViewWidth);
        return ix + iy * 16 + 17;
    }
}
